package com.hc.baselibray;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.example.baselibrary.ProcessConnection;
import com.example.baselibrary.R;

//守护进程，双进程通讯
public class GuardService extends Service {
    static String TAG="GuardService";
    private  final  int MessageId=20;
    public GuardService() {
    }
    /**
     * 创建通知通道
     * @param channelId
     * @param channelName
     * @return
     */
    private String createNotificationChannel(String channelId, String channelName){
        NotificationChannel chan = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            chan = new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_NONE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            chan.setLightColor(Color.BLUE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        }
        NotificationManager service = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            service.createNotificationChannel(chan);
        }
        return channelId;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String CHANNEL_ID = "11";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CHANNEL_ID = createNotificationChannel("kim.hsl.gogo", "ForegroundServicexxx");
        } else {
            CHANNEL_ID = "";
        }
        //通知的方式，提高进程的优先级
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Example Service")
                .setContentText("我切换成上帝了")
                .setSmallIcon(R.mipmap.ib_partner_focus)
                .build();
        startForeground(MessageId,notification);

        //绑定建立连接
        //重点2
        bindService(new Intent(GuardService.this,MyService.class),mServiceConnection,Service.BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
       //重点1
        return  new ProcessConnection.Stub() {};
    }
    private  ServiceConnection mServiceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //连接成功
            Log.d(TAG, "onServiceConnected: 连接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //断开,从新启动自己，重写绑定
            startService(new Intent(GuardService.this,MyService.class));
            //重新绑定
            bindService(new Intent(GuardService.this,MyService.class),mServiceConnection,Service.BIND_IMPORTANT);

        }
    };
}